package com.yimi;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@Slf4j
public class PushHttp {
    public static void main(String[] args) {
        sendSaveAxleTempurtureWarnInfo();
    }

    public static void sendSaveAxleTempurtureWarnInfo() {
        String url =
                "https://sfms-sit.yimidida.com/handler/saveAxleTempurtureWarnInfo";
        String clientKey = "repair_client_key";
        String nonceStr = UUID.randomUUID().toString();
        String timestamp = String.valueOf(System.currentTimeMillis());
        String secret =
                "41551e6b-6f4c-455e-8671-c7ed34e4f9dc";
        List<List<Map<String,String>>> datas = new ArrayList<>();
        List<Map<String, String>> items = new ArrayList<>();
        Map<String,String> data = new HashMap<>();
        data.put("carrierNo", "沪 GB2712");
        data.put("wheelPosition", "L2-2");
        data.put("sensorNo", "G8223500");
        data.put("sensorStatus", "1");
        data.put("tempTime", new Date().getTime()+"");
        data.put("createTime", new Date().getTime()+"");
        data.put("gpsTime", new Date().getTime()+"");
        data.put("altitude", "25");
        data.put("temp", "130");
        data.put("envTemp", "21");
        data.put("gpsLat", "12.6654");
        data.put("gpsLng", "121.452");
        data.put("speed", "60");
        data.put("carrierType", "4");
        data.put("alarmType", "1");
        items.add(data);
        datas.add(items);
        String paramStr = JSON.toJSONString(datas);
        String sign = generateSfmsSign(clientKey, secret, nonceStr, timestamp, paramStr);
        Map<String, String> headers = new HashMap<>();
        headers.put("clientKey", clientKey);
        headers.put("nonceStr", nonceStr);
        headers.put("timestamp", timestamp);
        headers.put("signature", sign);
//掉接口获取数据
        String resultStr = "";
        try {
            resultStr = HttpRequest.post(url).body(paramStr).addHeaders(headers).execute().body();
        } catch (Exception e) {
            log.error("请求失败", e);
        }
        log.info("接口返回的结果集是：" + resultStr);
    }
    /**
     * 生成签名(将 clientKey 、 clientSecret 、nonceStr 、timestamp 、 requestBody 依次以 | 字符连接)
     *
     * @param clientKey 文档中有* @param clientSecret 文档中有* @param nonceStr 自行生成一个随机字符串* @param timestamp 自行获取当前时间戳* @param requestBody（body 参数对象 json 字符串）*/
    public static String generateSfmsSign(String clientKey, String
            clientSecret, String nonceStr, String timestamp, String
                                                  requestBody) {
        String toSignContent = clientKey + "|" + clientSecret + "|" +nonceStr + "|" + timestamp + "|" + requestBody;
        log.info("追加 appSecret 后的待签名数据是:{}", toSignContent);
        String generatedSign =
                com.google.common.hash.Hashing.md5().hashString(toSignContent, com.google.common.base.Charsets.UTF_8).toString();
        log.info("生成的签名是:{}", generatedSign);
        return generatedSign;
    }

}
